package com.yanqu.road.server.manger.specialcookboy;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.specialcookboy.config.SpecialCookBoyConfig;
import com.yanqu.road.entity.activity.specialcookboy.sign.specialCookBoySignConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.log.specialcookboy.LogSpecialCookBoySevenDaySign;
import com.yanqu.road.pb.specialcookboy.SpecialCookBoyProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class SpecialCookBoySignMgr extends TempMgr {

    private static ActivityInfo activityInfo;

    private static specialCookBoySignConfig config;


    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload SpecialCookBoySevenSign activity start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.SpecialCookBoySevenSign.getValue());
        if (openActivityInfoList.size() <= 0){
            getLogger().info("reload SpecialCookBoySevenSign activity finish, no activity in show time.");
            config = null;
            activityInfo = null;
            return;
        }
        ActivityInfo tmpActivityInfo = openActivityInfoList.get(0);
        //活动配置
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityInfo.getActivityId());
        activityInfo = tmpActivityInfo;
        config = new specialCookBoySignConfig(activityConfigMap);
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static int simpleSign(GamePlayer player,int activityId,int conditionId) {
        if (activityInfo == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_NO_OPEN;
        }
        if (activityId != activityInfo.getActivityId()){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_ACTIVITY_ID_ERROR;
        }
        int calc2DateTDOADays = DateHelper.calc2DateTDOADays(new Date(activityInfo.getBeginTime() * 1000), new Date());
        if (calc2DateTDOADays < 0 || calc2DateTDOADays > 7) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_OVER_TIME;
        }
        //拿出condition
        BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(activityInfo.getActivityId());
        if (baseActivityData == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_CONDITION_NO_EXIST;
        }
        BaseActivityCondition activityCondition = baseActivityData.getBaseActivityCondition(conditionId);
        if (activityCondition == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_CONDITION_NO_EXIST;
        }
        int checkCondition = checkCondition(activityCondition, calc2DateTDOADays);
        if (checkCondition != 0){
            return checkCondition;
        }

        //加奖励
        String reward = addAndGetCondition(player, activityCondition);

        SpecialCookBoyProto.SpecialCookBoySevenDaySignResp.Builder builder = SpecialCookBoyProto.SpecialCookBoySevenDaySignResp.newBuilder();
        builder.setRet(0);
        builder.setReward(reward);
        player.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_SIGN,builder);
        LogSpecialCookBoySevenDaySign logSpecialCookBoySevenDaySign = new LogSpecialCookBoySevenDaySign(activityId,player.getUserId(),calc2DateTDOADays,calc2DateTDOADays,reward,0,new Date());
        AutoLogMgr.add(logSpecialCookBoySevenDaySign);
        return 0;

    }

    public static int signPastDay(GamePlayer gamePlayer,int activityId,int conditionId,int day) {
        if (activityInfo == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_NO_OPEN;
        }
        if (activityId != activityInfo.getActivityId()){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_ACTIVITY_ID_ERROR;
        }
        if (day<=0 || day>7){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_OVER_TIME;
        }
        int calc2DateTDOADays = DateHelper.calc2DateTDOADays(new Date(activityInfo.getBeginTime() * 1000), new Date());
        if (calc2DateTDOADays < 0) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_OVER_TIME;
        }

        //拿出condition
        BaseActivityData baseActivityData = gamePlayer.getModule(ActivityModule.class).getBaseActivityData(activityInfo.getActivityId());
        if (baseActivityData == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_CONDITION_NO_EXIST;
        }
        BaseActivityCondition activityCondition = baseActivityData.getBaseActivityCondition(conditionId);
        if (activityCondition == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_CONDITION_NO_EXIST;
        }
        if (day >= calc2DateTDOADays){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_CONDITION_ERROR;
        }
        //检查condition是否正常
        int checkCondition = checkCondition(activityCondition, day);
        if (checkCondition != 0){
            return checkCondition;
        }
        //检测道具
        CurrencyModule module = gamePlayer.getModule(CurrencyModule.class);
        if (!module.currencyIsEnough(config.getProperty())){
            return GameErrorCode.E_SPECIAL_COOK_BOY_RE_SIGN_ITEM_NO_ENOUGH;
        }

        //口道具
        module.removeCurrency(config.getProperty(), eLogMoneyType.SpecialCookBoySign,eLogMoneyType.SpecialCookBoyReSignCost);
        //加奖励
        String reward = addAndGetCondition(gamePlayer, activityCondition);


        SpecialCookBoyProto.SpecialCookBoySevenDaySignAgainResp.Builder builder = SpecialCookBoyProto.SpecialCookBoySevenDaySignAgainResp.newBuilder();
        builder.setRet(0);
        builder.setReward(reward);
        gamePlayer.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_SIGN_PAST_DAY,builder);
        LogSpecialCookBoySevenDaySign logSpecialCookBoySevenDaySign = new LogSpecialCookBoySevenDaySign(activityId,gamePlayer.getUserId(),day,calc2DateTDOADays,reward,1,new Date());
        AutoLogMgr.add(logSpecialCookBoySevenDaySign);

        return 0;
    }

    public static int checkCondition(BaseActivityCondition activityCondition,int day){

        if (activityCondition.getConditionType() != eGamePlayerEventType.SpecialCookBoySevenDaySign.getValue()){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_CONDITION_ERROR;
        }

        if (activityCondition.isGetReward()){               //已经领奖
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_CONDITION_HAS_GET_REWARD;
        }

        if (activityCondition.getActivityConditionInfo().getParamList().get(2).intValue() != day){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SIGN_CONDITION_DAY_ERROR;
        }

        return 0;
    }

    public static String addAndGetCondition(GamePlayer gamePlayer,BaseActivityCondition activityCondition){
        activityCondition.setValue(BigInteger.ONE);
        String reward = activityCondition.getActivityConditionInfo().getRewardStr();
        Property property = PropertyHelper.parseStringToProperty(reward);
        int patronId = config.getPatronId();
        if (property.getGoods().containsKey(patronId)){
            if (gamePlayer.getModule(PatronsModule.class).getUserPatrons(patronId) != null){
                property.removeProperty(patronId);
                String patronTransToItem = config.getPatronTransToItem();
                Property transTo = PropertyHelper.parseStringToProperty(patronTransToItem);

                String activityName = ServerLanguageMgr.getContent(activityInfo.getActivityName(), gamePlayer.getLanguage());
                String mailTitle = ServerLanguageMgr.getContent(config.getMailTitle(),gamePlayer.getLanguage());

                String goodsName ="";
                int goodNum = 0;
                for (Map.Entry<Integer,BigInteger> good:transTo.getGoods().entrySet()){
                    GoodsInfo goodsInfo = GoodsMgr.getGoodsById(good.getKey());

                    if(goodsInfo != null){
                        goodsName = goodsInfo.getGoodsName();
                        goodsName = MultipleLanguageMgr.getContent(goodsName, gamePlayer.getLanguage());
                    }else {
                        goodsName = MultipleLanguageMgr.getContent("Items-" + good.getKey(), gamePlayer.getLanguage());
                    }
                    goodNum = good.getValue().intValue();

                }
                String patronsName = MultipleLanguageMgr.getContent("Patrons-" + config.getPatronId(), gamePlayer.getLanguage());

                String mailContent = StringUtils.format(ServerLanguageMgr.getContent(config.getMailContent(),gamePlayer.getLanguage()),activityName,patronsName,goodsName,goodNum);

                MailManager.sendMail(gamePlayer.getUserId(), eMailType.SYSTEM.getValue(), patronTransToItem, mailContent, mailTitle);
            }
        };
        activityCondition.setGetReward(true);
        activityCondition.removeTrigger(gamePlayer);
        gamePlayer.getModule(CurrencyModule.class).addCurrency(property,eLogMoneyType.SpecialCookBoySign,eLogMoneyType.SpecialCookBoyReSignReward);
        return PropertyHelper.parsePropertyToString(property);
    }

}
